﻿using DocumentGeneratorPlugins;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SQLDocumentor.UI
{
    /// <summary>
    /// Interaction logic for SQLDocumentorUI.xaml
    /// </summary>
    public partial class SQLDocumentorUI : UserControl
    {
        ISystemDocumentor _CurrentObj;
        string docPath;
        string strAuthor;
        string strDescription;
        string strTitle;
        string strSubject;
        string strInstance;
        bool bSQLAuth;
        string SQLUserName;
        string SQLPassword;
        public SQLDocumentorUI()
        {
            InitializeComponent();
        }
        public SQLDocumentorUI(ISystemDocumentor doc)
        {
            InitializeComponent();
            _CurrentObj = doc;
            _CurrentObj.DocumentGeneratorEvent += _CurrentObj_DocumentGeneratorEvent;
            txtAuthor.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            
        }

        void _CurrentObj_DocumentGeneratorEvent(object sender, DocumentGeneratorEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
           lblTarget.Content = e.Message);
            if (e.State == StateStep.Start)
                this.Dispatcher.Invoke(() =>
                {
                    pBar.IsIndeterminate = true;
                });

            if (e.State == StateStep.Finish)
                this.Dispatcher.Invoke(() =>
                {
                    pBar.IsIndeterminate = false;
                    pBar.Value = 100;
                    wzDocGen.CurrentPage = EndWizard;
                });
        }

        private void StartDocumentGeneration()
        {
            if (_CurrentObj == null)
                return;
            SQLDocumentOption docOptions;
            docOptions = _CurrentObj.CreateDocumentOption() as SQLDocumentOption;
            if (docOptions == null)
                throw new Exception("Probléme avec les options du document");
            docOptions.DocumentPath = docPath;
            docOptions.DocumentPropertiesCreator = strAuthor;
            docOptions.DocumentPropertiesDescription = strDescription;
            docOptions.DocumentPropertiesLastModifiedBy = strAuthor;
            docOptions.DocumentPropertiesSubject = strSubject;
            docOptions.DocumentPropertiesTitle = strTitle;
            docOptions.Instance = strInstance;
            docOptions.PasswordSQL = SQLPassword;
            docOptions.SQLAuthentication = bSQLAuth;
            docOptions.UserNameSQL = SQLUserName;
            _CurrentObj.GenerateDocument(docOptions);
        }

        private void wzDocGen_Finish(object sender, RoutedEventArgs e)
        {
            wzDocGen.CurrentPage = Terminated;
        }

        private void wzDocGen_PageChanged(object sender, RoutedEventArgs e)
        {
            if (wzDocGen.CurrentPage == Terminated)
            {
                strSubject = string.IsNullOrWhiteSpace(txtSubject.Text) ? "Audit fait avec SPToolKit" : txtSubject.Text;
                strDescription = txtDescription.Text;
                strAuthor = string.IsNullOrWhiteSpace(txtAuthor.Text) ? "System" : txtAuthor.Text;
                docPath = lblPath.Content == null ? "Audit.docx" : lblPath.Content.ToString();
                strTitle = string.IsNullOrWhiteSpace(txtTitle.Text) ? "Audit fait avec SPToolKit" : txtTitle.Text;
                strInstance = string.IsNullOrWhiteSpace(txtInstance.Text) ? "localhost" : txtInstance.Text;
                bSQLAuth = chkAuthSQL.IsChecked.HasValue?chkAuthSQL.IsChecked.Value: false;
                SQLUserName=txtUserName.Text;
                SQLPassword = txtPassword.Text;
                System.Threading.ThreadStart ts = new System.Threading.ThreadStart(StartDocumentGeneration);
                System.Threading.Thread th = new System.Threading.Thread(ts);
                th.Start();
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.DefaultExt = ".docx";
            sfd.Filter = "Document Word (.docx)|*.docx";
            if (sfd.ShowDialog() == true)
            {
                lblPath.Content = sfd.FileName;
            }
        }
    }
}
